package labelsystem.Controller;/**
 * @projectName careOlder
 * @Description TODO
 * @author MCG
 * @time 2023/3/28 14:20
 */

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import labelsystem.Pojo.*;
import labelsystem.Service.volunteerService;
import labelsystem.Service.yuserService;
import labelsystem.utils.returnMSG;
import org.omg.CORBA.portable.ValueOutputStream;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *description: TODO
 *@author MCG
 *@date 2023/3/28 14:20
 *@Version 1.0
 */
@Api("护工处理模块")
@RestController
@CrossOrigin
public class volunteerHandler {
    @Resource
    volunteerService volunteerService;
    @Resource
    private RedisTemplate<String,String> redisTemplate;


    @PostMapping("loginPasswordV")
    @ApiOperation("密码登陆:phone,password")
    public returnMSG loginInPassword(@RequestParam("phone") String phone, @RequestParam("password") String password){
        boolean flag=volunteerService.loginInByPassword(phone,password);
        if(flag==true){
            Volunteer volunteer=volunteerService.queryInfoByTel(Long.parseLong("phone"));
            returnMSG returnMSG= labelsystem.utils.returnMSG.success();
            returnMSG.add("volunteer",volunteer);
            return returnMSG;
        }else{
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("密码输入错误");
            return returnMSG;
        }
    }


    @PostMapping("/resetV")
    @ApiOperation("重置密码:phone,verifyCode,newpassword,confirmpassword")
    public returnMSG loginInCode(@RequestParam("phone") String phone,@RequestParam("verifyCode") String verifyCode,@RequestParam("newpassword") String newpassword,@RequestParam("confirmpassword") String password){
        if(!newpassword.equals(password)){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("请输入一致的密码");
            return returnMSG;
        }
        //从redis里面取到验证码
        String code = redisTemplate.opsForValue().get(phone);
        if(volunteerService.loginInByCode(verifyCode,code)){
            String reg="^(?=.*\\d)(?=.*[A-z])[\\da-zA-Z]{1,9}$";
            boolean flag=newpassword.matches(reg);
            if(flag){
                returnMSG returnMSG= labelsystem.utils.returnMSG.success();
                Volunteer volunteer=volunteerService.queryInfoByTel(Long.parseLong(phone));
                returnMSG.add("volunteer",volunteer);
                return returnMSG;
            }else{
                returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
                returnMSG.setMsg("重置密码不符合格式");
                return returnMSG;
            }

        }else{
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("验证码错误");
            return returnMSG;
        }
    }

    @PostMapping("/registerV")
    @ApiOperation("注册:name,phone,password,age,gender,experience")
    public returnMSG registerUser(@RequestParam("name")String name,@RequestParam("phone") String phone,@RequestParam("password") String password,
                                  @RequestParam("age") String age,@RequestParam("gender") String gender,
                                  @RequestParam("experience") @ApiParam("工作经历") String experience){
        String reg="^(?=.*\\d)(?=.*[A-z])[\\da-zA-Z]{1,9}$";
        boolean flag=password.matches(reg);
        if(!flag){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("请输入合法的密码格式");
            return returnMSG;
        }else{
            Volunteer volunteer=volunteerService.register(name,Long.parseLong(phone),Integer.parseInt(age),gender,password,Float.parseFloat("45.0"),Float.parseFloat("5.0"),Integer.parseInt(experience),"");
            returnMSG returnMSG= labelsystem.utils.returnMSG.success();
            returnMSG.add("volunteer",volunteer);
            return returnMSG;
        }

    }
    @GetMapping("/queryOrdersV")
    @ApiOperation("id查询订单:vid")
    public returnMSG queryOrders(@RequestParam("vid") String vid){
        List<Order> orders=volunteerService.selectListById(Long.parseLong(vid));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("orders",orders);
        return returnMSG;
    }
    @GetMapping("/queryTextsV")
    @ApiOperation("id查询发布文章:vid")
    public returnMSG queryTexts(@RequestParam("vid") String vid){
       List<Text>texts=volunteerService.selectTextById(Long.parseLong(vid));
       returnMSG returnMSG= labelsystem.utils.returnMSG.success();
       returnMSG.add("texts",texts);
       return returnMSG;
    }

    @GetMapping("/queryVideoV")
    @ApiOperation("id查询发布视频:vid")
    public returnMSG queryVideo(@RequestParam("vid") String vid){
        List<Video> videos=volunteerService.selectVideoById(Long.parseLong(vid));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("videos",videos);
        return returnMSG;
    }

    @PostMapping("/queryInfoV")
    @ApiOperation("id查询个人信息：vid")
    public returnMSG queryInfo(@RequestParam("vid") String vid){
        Volunteer volunteer=volunteerService.queryInfo(Long.parseLong(vid));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("volunteer",volunteer);
        return returnMSG;
    }


    @PostMapping("/updatePasswordV")
    @ApiOperation("更新密码:phone,password")
    public returnMSG updatePassword(@RequestParam("phone") String phone,@RequestParam("password") String password){
        Volunteer volunteer=volunteerService.queryInfoByTel(Long.parseLong(phone));
        volunteer.setPassword(password);
        boolean flag=volunteerService.updateVolunteer(volunteer);
        if(flag){
            return returnMSG.success();
        }else{
            return returnMSG.fail();
        }
    }

}
